import wx
from PublicMethod import *
import  wx.lib.buttons  as  buttons
'''
SelMaster_Selector :
    
    The selector of master
'''
class SelMaster_Selector():
    '''
    Constructor : SelMaster_Selector
        panel - this object is added to panel
    '''
    def __init__(self, panel):
        self._001_initialInstanceVariable(panel)
        self._002_createObject()
        self._005_createNextBeforeImg()
        self._007_createImgFly()
        self._008_createManImageBorder()
    '''
    function _001 : initialize the variables used for this object
        panel - this object is added to panel
    '''
    def _001_initialInstanceVariable(self, panel):
        #size of player image
        self.sizePlayer = (70,70)
        #pointer of main frame
        self.panel = panel
        #current selecting index
        self.curSelIndex = 0
        #list of player
        self.listPlayerName = PublicMethod.ACT005_getDirInSpecDir(DIR_MASTER)
        #list of image player
        self.listImgPlayer = []
        #label of player name
        self.labelPlayerName = None
        #label of image before
        self.imgBefore = None
        #label of image next
        self.imgNext = None
        #interval of player image
        self.interval = 0
        #start position of player image
        self.startPlayerPos = None 
        #list image of fly
        self.listImgFly = []
        #list of border image
        self.listBorder = []
    '''
    function _002 : create all object of selector
    '''
    def _002_createObject(self):
        self._004_clearPlayerObj()
        #player image
        index = 0
        self.interval = 30
        self.startPlayerPos = ((self.panel.GetSize()[0] - LIST_FILENAME_MASTER.__len__() * (self.sizePlayer[0] + self.interval)) / 2, 350)
        while index < LIST_FILENAME_MASTER.__len__() :
            fileName = DIR_MASTER + "/" + self.listPlayerName[self.curSelIndex] + "/" + LIST_FILENAME_MASTER[index]
            image = PublicMethod.ACT001_getBmpImage(fileName, self.sizePlayer[0], self.sizePlayer[1])
            btn = wx.StaticBitmap(self.panel, -1, image, pos = (-300, -300))
            btn.SetPosition((self.startPlayerPos[0] + index * (self.sizePlayer[0] + self.interval), self.startPlayerPos[1]))
            btn.Show(False)
            btn.Show(True)
            self.listImgPlayer.append(btn)
            index = index + 1
        #player title image
        self.labelPlayerName = buttons.GenButton(self.panel, -1, self.listPlayerName[self.curSelIndex])
        self.labelPlayerName.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD, False))
        self.labelPlayerName.SetBezelWidth(5)
        self.labelPlayerName.SetMinSize(wx.DefaultSize)
        self.labelPlayerName.Enable(False)
        self.labelPlayerName.SetBackgroundColour("#FF1C19")
        self.labelPlayerName.SetForegroundColour("cyan")
        self.labelPlayerName.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.labelPlayerName.SetSize((250, 40))
        self.labelPlayerName.SetPosition(((self.panel.GetSize()[0] - self.labelPlayerName.GetSize()[0]) / 2 - 10, 200))
    '''
    function _003 : set image and name of current selected player
    '''
    def _003_setToCurSelPlayer(self):
        index = 0
        while index < LIST_FILENAME_MASTER.__len__() :
            fileName = DIR_MASTER + "/" + self.listPlayerName[self.curSelIndex] + "/" + LIST_FILENAME_MASTER[index]
            image = PublicMethod.ACT001_getBmpImage(fileName, self.sizePlayer[0], self.sizePlayer[1])
            self.listImgPlayer[index].SetBitmap(image)
            index = index + 1
        self.labelPlayerName.SetLabel(self.listPlayerName[self.curSelIndex])
        self.labelPlayerName.Show(False)
        self.labelPlayerName.Show(True)
    '''
    function _004 : clear all object of list - listImgPlayer and title
    '''
    def _004_clearPlayerObj(self):
        #destroy all image of player
        index = 0
        while index < self.listImgPlayer.__len__() :
            self.listImgPlayer[index].Destroy()
            index = index + 1
        self.listImgPlayer = []
        #destroy title image
        if self.labelPlayerName != None :
            self.labelPlayerName.Destroy()
            self.labelPlayerName = None
    '''
    function _005 : create object image of next and before
    '''
    def _005_createNextBeforeImg(self):
        #create before image
        image = PublicMethod.ACT001_getBmpImage(DIR_IMG_SEL_PLAYER + "/before.png")
        self.imgBefore = wx.BitmapButton(self.panel, -1, image, pos = (self.startPlayerPos[0] - 150, self.startPlayerPos[1] - 100), style = wx.FRAME_SHAPED)
        self.imgBefore.Enable(False)
        self.imgBefore.Show(False)
        #create next image
        image = PublicMethod.ACT001_getBmpImage(DIR_IMG_SEL_PLAYER + "/next.png")
        self.imgNext = wx.BitmapButton(self.panel, -1, image, pos = (self.panel.GetSize()[0] - 150, self.startPlayerPos[1] - 100), style = wx.FRAME_SHAPED)
        self.imgNext.Enable(False)
        if self.listImgPlayer.__len__() < 2 :
            self.imgNext.Show(False)
    '''
    function _006 : set enable of before and next image
    '''
    def _006_setImgDirection(self):
        if self.curSelIndex == 0 : #no before image
            self.imgBefore.Show(False)
        else :
            self.imgBefore.Show(True)
        if self.curSelIndex < self.listPlayerName.__len__() - 1 : #no next image
            self.imgNext.Show(True)
        else :
            self.imgNext.Show(False)
    '''
    function _007 : create image of fly to decorate the label of player name
    '''
    def _007_createImgFly(self):
        pos_labelPlayerName = self.labelPlayerName.GetPosition()
        size_labelPlayerName = self.labelPlayerName.GetSize()
        list = [["leftFly.png", (pos_labelPlayerName[0] - 127, pos_labelPlayerName[1] - 60)],
                ["rightFly.png", (pos_labelPlayerName[0] + size_labelPlayerName[0] - 3, pos_labelPlayerName[1] - 56)]]
        for fileName, position in list :
            image = PublicMethod.ACT001_getBmpImage(DIR_IMG_SEL_PLAYER + "/" + fileName)
            btn = wx.BitmapButton(self.panel, -1, image, pos = position, style = wx.FRAME_SHAPED)
            btn.Enable(False)
            self.listImgFly.append(btn)
            
    def _008_createManImageBorder(self):
        index = 0
        rough = 2
        while index < 4 :
            pos = (self.listImgPlayer[index].GetPosition()[0] - rough, self.listImgPlayer[index].GetPosition()[1] - rough)
            size = (self.listImgPlayer[index].GetSize()[0] + rough, self.listImgPlayer[index].GetSize()[1] + rough)
            border = PublicMethod.ACT006_createABorder(self.panel, pos, size, rough, "cyan")
            self.listBorder.append(border)
            index = index + 1
    '''
    function ACT001 : show next image
    '''
    def ACT001_showNext(self):
        if self.curSelIndex < self.listPlayerName.__len__() - 1 :
            self.curSelIndex = self.curSelIndex + 1
            self._003_setToCurSelPlayer()
            self._006_setImgDirection()
    '''
    function ACT001 : show before image
    '''
    def ACT002_showBefore(self):
        if self.curSelIndex > 0 :
            self.curSelIndex = self.curSelIndex - 1
            self._003_setToCurSelPlayer()
            self._006_setImgDirection()
    '''
    function ACT003 : destroy all object of this class
    '''
    def ACT003_destroyAll(self):
        #clear image player
        index = 0
        while index < self.listImgPlayer.__len__() :
            self.listImgPlayer[index].Destroy()
            index = index + 1
        #clear player name
        del self.listPlayerName
        #clear label of player
        self.labelPlayerName.Destroy()
        #clear image before and next
        self.imgBefore.Destroy()
        self.imgNext.Destroy()
        #clear image of fly
        index = 0
        while index < self.listImgFly.__len__() :
            self.listImgFly[index].Destroy()
            index = index + 1
        #clear Border
        while self.listBorder.__len__() > 0 :
            while self.listBorder[0].__len__() > 0 :
                self.listBorder[0][0].Destroy()
                self.listBorder[0].__delitem__(0)
            self.listBorder.__delitem__(0)
        del self
    '''
    function GET001 : get name of current chosen player
    '''
    def GET001_curSelectPlayerName(self):
        return self.listPlayerName[self.curSelIndex]
        